package mazegame;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class ServerUI extends JFrame {

	private JLabel lblMapSize = new JLabel("Map Size");
	private JLabel lblTreasureCount = new JLabel("Treasure Number");
	private JTextField txtMapSize = new JTextField(5);
	private JTextField txtTreasureCount = new JTextField(5);
	private JButton btnStartServer = new JButton("Start Server");
	private JButton btnCreateClient = new JButton("Create Client");
	public JTextArea txtStatusView = new JTextArea(20, 50);
	
	public GameStartTimer startTimer = null;
	public int gameStartWaitTime = 5;
	public int gameStartTickcount;
	public Constants.ServerStatus gameStatus = null;



	public ServerUI() {
		
		super("Maze Game Server");

		txtMapSize.setText("5");
		txtTreasureCount.setText("20");
		txtStatusView.setText(MazeUtil.log("ready", ""));

		btnStartServer.addActionListener(new ServerButtonListener());
		btnCreateClient.addActionListener(new ServerButtonListener());
	}

	private class ServerButtonListener implements ActionListener {
		public synchronized void actionPerformed(ActionEvent e) {
			
			if (e.getSource() == btnCreateClient)
            {
                ClientUI client = new ClientUI();
                client.displayClientUI();
            }
			else if(e.getSource()== btnStartServer)
			{

                try
                {
                	gameStatus=Constants.ServerStatus.SERVER_IDLE;
                	gameStartTickcount = gameStartWaitTime;
                    String name = "MazeServerImpl";
                    MazeServerImpl engine = new MazeServerImpl(ServerUI.this);
                    engine.setNextPlayerID(1);
                    engine.setPlayerCount(0);
                    engine.setMapSize(Integer.parseInt(txtMapSize.getText()));
                    engine.setTreasureNum(Integer.parseInt(txtTreasureCount.getText()));
                    engine.setTreasureCount(Integer.parseInt(txtTreasureCount.getText()));
                    MazeServerInterface stub = (MazeServerInterface) UnicastRemoteObject.exportObject(engine, 0);
                    Registry registry = LocateRegistry.createRegistry(2001);
                    registry.rebind(name, stub);
                    txtStatusView.setText(MazeUtil.log("rmi registry bound succeed", txtStatusView.getText()));
                }
                catch (Exception e2)
                {
                	txtStatusView.setText(MazeUtil.log("ERROR: rmi registry bound failed", txtStatusView.getText()));
                    e2.printStackTrace();
                    return;
                }

                txtStatusView.setText(MazeUtil.log("server is running now", txtStatusView.getText()));
            
			}
		}
	}

	public void updateGameStartTick() {

		System.out.println("UpdateGameStartTick: gameStatus >>> "+gameStatus);
		System.out.println("UpdateGameStartTick: gameStartTickcount >>> "+gameStartTickcount);

		if (gameStatus == Constants.ServerStatus.SERVER_GAME_STARTING
				&& gameStartTickcount > 0) {
			gameStartTickcount -= 1;
			System.out.println("UpdateGameStartTick SERVER_GAME_STARTING:"+gameStatus);
		} else if (gameStatus == Constants.ServerStatus.SERVER_GAME_STARTING
				&& gameStartTickcount <= 0) {
			startTimer.timer.cancel();
			gameStartTickcount = gameStartWaitTime;
			gameStatus = Constants.ServerStatus.SERVER_GAME_RUNNING;
			System.out.println("UpdateGameStartTick SERVER_GAME_RUNNING:"+gameStatus);
			System.out.println("gameStartTickcount:"+gameStartTickcount);
			txtStatusView.setText(MazeUtil.log("game started", txtStatusView.getText()));
		}
	}
	
	public void displayServerUI() {
		txtStatusView.setEditable(false);

		JPanel topPanel = new JPanel();
		topPanel.setLayout(new FlowLayout());
		topPanel.add(lblMapSize);
		topPanel.add(txtMapSize);
		topPanel.add(lblTreasureCount);
		topPanel.add(txtTreasureCount);
		topPanel.add(btnStartServer);
		topPanel.add(btnCreateClient);
		setLayout(new FlowLayout());
		add(topPanel);
		add(txtStatusView);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(650, 420);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		ServerUI server = new ServerUI();
		server.displayServerUI();
	}

}
